/*
 * =====================================================================================
 *
 *       Filename:  buffer.h
 *
 *    Description:  declare the buffer struct and some operating functions
 *
 *        Version:  1.0
 *        Created:  2011年12月08日 10时40分35秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Regan (), lcqhigh@gmail.com
 *
 * =====================================================================================
 */
#ifndef BUFFER_H
#define BUFFER_H

#include	<stdint.h>

struct buffer_s
{
    int32_t buflen;
    int32_t readptr;
    int32_t datalen;
    int32_t peekptr;  //相对于readptr的位移
    char*    data;
};

typedef struct buffer_s     buffer_t;

buffer_t *buffer_new(int buflen);

void buffer_free(buffer_t *buf);


int buffer_write(buffer_t *buf, const void *data, int datlen);

int buffer_read(buffer_t *buf, void *data, int datlen);

int buffer_peek(buffer_t *buf, void *data, int datlen);

int buffer_seek(buffer_t *buf, int offset, int whence);

int buffer_rewind(buffer_t *buf);

void buffer_clear(buffer_t *buf);

int buffer_drain(buffer_t *buf, int start, int len);

int buffer_movebuf(buffer_t *dst, buffer_t *src);

int buffer_search(buffer_t *buf, const char *what, int len, int start);

int buffer_search_all(buffer_t *buf, const char *what, int len);

int buffer_regmatch(buffer_t *buf, const char *regex, int start, int *off, int *end);

int buffer_realign(buffer_t *buf);

int buffer_expand(buffer_t *buf, int datlen);

void buffer_print(buffer_t *buf);

size_t readable_bytes();
#endif
